package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.exception.BusinessRuntimeException;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    CheckItemDao checkItemDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        Page<CheckItem> pages = checkItemDao.findByCondition(queryPageBean.getQueryString());

        return new PageResult(pages.getTotal(),pages);
    }

    @Override
    @Transactional
    public void add(CheckItem checkItem) {
        log.debug("service层获得了web层的传参："+checkItem);
        checkItemDao.add(checkItem);
    }

    @Override
    @Transactional
    public void del(Integer id) {
        int count = checkItemDao.findCountById(id);
        if (count>0){
            throw new BusinessRuntimeException("该检查项被检查组关联,不能删除！！！");
        }
        checkItemDao.delById(id);
    }

    @Override
    public CheckItem findById(Integer id) {
        CheckItem byId = checkItemDao.findById(id);
        return byId;
    }

    @Override
    @Transactional
    public void editById(CheckItem checkItem) {
        checkItemDao.editById(checkItem);
    }

    @Override
    public List findAll() {
        List<CheckItem> all = checkItemDao.findAll();
        return all;
    }


}
